package com.happyfamily.springboot.crowdsourcingplatform.dao.pan;

import com.happyfamily.springboot.crowdsourcingplatform.base.result.Results;
import com.happyfamily.springboot.crowdsourcingplatform.dto.NeedDto;
import com.happyfamily.springboot.crowdsourcingplatform.model.Need;
import com.happyfamily.springboot.crowdsourcingplatform.model.ServerType;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import org.apache.ibatis.annotations.*;

import java.lang.reflect.Type;
import java.util.List;

/**
 * @Author Administrator
 * @create 2020/02/15 23:32
 * 禁止郭文忠进入
 */
@Mapper
public interface UserDao
{


	/**
	 * 查询手机号是否存在
	 * @param telephone
	 * @return
	 */
	@Select("select * from sys_user t where t.uphone = #{uphone}")
	SysUser getUserByPhone(String telephone );

	/**
	 * 插入用户数据
	 * 	//@SelectKey(statement = "SELECT LAST_INSERT_ID",keyProperty = "id",resultType = Long.class,before = false)//返回非自增主键
	 *
	 * @param sysUser 用户信息
	 * @return
	 */
	@Insert("insert into sys_user(uaccount, uname, upassword, uphone,uaddress,sid,rid, usex) values(#{uaccount}, #{uname}, #{upassword}, #{uphone},#{uaddress},#{sid}, #{rid}, #{usex})")
	@Options(useGeneratedKeys = true,keyProperty = "id") //返回自增的主键id
	int save(SysUser sysUser);


	/**
	 * 根据用户账号查询用户信息
	 * @param uaccount
	 * @return
	 */
    @Select("select * from sys_user t where t.uaccount= #{uaccount}")
	SysUser getUser(String uaccount);


	/**
	 * 根据sparentid查询父级
	 * @param sparentid
	 * @return
	 */
	@Select("select * from server_type t where t.sparentid =#{sparentid}")
	List<ServerType> getById(@Param("sparentid")long sparentid);

	/**
	 * 根据sid查询子级
	 * @param sid
	 * @return
	 */
	@Select("select * from server_type t where t.sparentid=#{sid}")
	List<ServerType> getStatus(@Param("sid")long sid);

	/**
	 * 保存雇主发布的需求
	 * @param need
	 * @param nuseraccount
	 * @return
	 */
	@Insert("insert into need (ntitle, ncontent, ndemand, nmoney,nendtime, nrealtime,sname,nuseraccount) values(#{need.ntitle},#{need.ncontent},#{need.ndemand},#{need.nmoney},#{need.nendtime},#{need.nrealtime},#{need.sname},#{nuseraccount})")
	int saveUserPublishing(@Param("need")Need need,@Param("nuseraccount")String nuseraccount);


	/**
	 * 选择服务类型名称
	 * @param typename
	 * @return
	 */
	@Select("select t.sname from server_type t where t.sid=#{typename}")
	String getSnameBySid(String typename);




	/**
	 * 查询所有的服务商
	 * @param rid
	 * @return
	 */
	@Select("select * from sys_user t where t.rid=#{rid}")
	List<SysUser> getByRid(Integer rid);












	//----------------------郭老板写的DAO----------------------------------------------------

	@Select("SELECT * FROM sys_user su,sys_role sr,parameter p,server_type st " +
			"WHERE su.rid=sr.id and su.sid=st.sid and su.pid=p.pid and  sr.id=3 " +
			"and st.sid=1 order by su.uid limit #{startPosition},#{limit}")
	List<SysUser> getList(@Param("startPosition") Integer startPosition, @Param("limit") Integer limit);

	@Select("SELECT COUNT(*) FROM sys_user where rid=3")
	int getSum();

	@Update(" UPDATE sys_user SET upassword=1111 WHERE uid=#{uid}")
	int resetPassword(@Param("uid") Long uid);

	@Update(" UPDATE sys_user SET pid=#{pid} WHERE uid=#{uid}")
	int changeStates(@Param("uid") Long uid,@Param("pid") Long pid );

	@Select("SELECT * FROM sys_user su,sys_role sr,parameter p,server_type st " +
			"WHERE su.rid=sr.id and su.sid=st.sid and su.pid=p.pid and  sr.id=3 and " +
			"st.sid=1 and su.uname like '%${uname}%' order by su.uid limit #{startPeople},#{limit}")
	List<SysUser> getSpecifyList(String uname,@Param("startPeople") Integer startPeople, @Param("limit") Integer limit);

	@Select("SELECT COUNT(*) FROM sys_user where rid=3  and uname like '%${uname}%'")
	int getSpecifySum(String uname);

	@Select("SELECT pname from parameter where pid=#{pid}")
	String getEmployersStates(Long pid);

	@Select("SELECT * FROM sys_user su WHERE su.rid  in (#{rid1},#{rid2}) order by su.uid limit #{startPosition},#{limit}")
	List<SysUser> getServiceProvidersList(Integer rid1,Integer rid2,@Param("startPosition") Integer startPosition, @Param("limit") Integer limit);

	@Select("SELECT COUNT(*) FROM sys_user where rid in (#{rid1},#{rid2})")
	int getServiceProvidersSum(Integer rid1,Integer rid2);

	@Select("SELECT description from sys_role where id=#{id}")
	String getServiceProvidersStates(Integer id);



}
